<template>
  <view>
    <u-popup v-model="showDialog" mode="center" border-radius="10" closeable>
      <view class="ver-layout-center" style="margin: 40px">
        <view style="margin-bottom: 10px">下载进度：</view>
        <u-circle-progress active-color="#2979ff" :percent="downProgress" width="100" border-width="7">
          <view>{{ downProgress }}%</view>
        </u-circle-progress>
      </view>
    </u-popup>
    <button @click="checkUpdate" type="warning">检查更新</button>
  </view>
</template>

<script>
export default {
  name: "check-update",
  data() {
    return {
      downloadUrl: 'https://bucket-zyj001.oss-cn-wulanchabu.aliyuncs.com/mdej_2.1.7%281%29.apk',
      showDialog: false,
      downProgress: 0
    }
  },
  methods: {
    checkUpdate() {
      this.showDialog = true
      let dTask = plus.downloader.createDownload(this.downloadUrl, {}, (d, status) => {
        if (status == 200) {
          plus.runtime.install(
              d.filename,
              {filename: `_downloads/${d.filename}`},
              function () {
                this.showDialog = false
              },
              function (e) {
                // 安装更新文件失败
                plus.nativeUI.alert('更新文件失败')
              }
          )
        } else {
          plus.nativeUI.alert('下载文件失败')
        }
      })
      dTask.addEventListener(
          'statechanged',
          (download, status) => {
            if (download.state == 3) {
              let num = (download.downloadedSize / download.totalSize) * 100
              this.downProgress = parseInt(num)
            }
          },
          false
      )
      dTask.start() //执行下载
    }
  }
}
</script>

<style scoped>

</style>
